package com.ronglian.iot.config;

import com.ronglian.iot.common.handler.GlobalExceptionHandler;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.ArrayList;
import java.util.Collections;

/**
 *
 * @author ronglianAdmin
 */
@Configuration("admimWebConfig")
@Primary
public class WebConfiguration implements WebMvcConfigurer {

    @Value("${spring.profiles.active}")
    private String active;

    @Bean
    GlobalExceptionHandler getGlobalExceptionHandler() {
        return new GlobalExceptionHandler();
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
//        registry.addInterceptor(getPermissionRestInterceptor())
//                .addPathPatterns(getIncludePathPatterns())
//                .excludePathPatterns(getNotIncludePathPatterns());
//        registry.addInterceptor(getUserAuthRestInterceptor())
//                .addPathPatterns(getIncludePathPatterns())
//                .excludePathPatterns(getNotIncludePathPatterns());

    }

    //服务鉴权
//    @Bean
//    ServiceAuthRestInterceptor getServiceAuthRestInterceptor() {
//        return new ServiceAuthRestInterceptor();
//    }

    //用户授权
//    @Bean
//    PermissionRestInterceptor getPermissionRestInterceptor() {
//        return new PermissionRestInterceptor();
//    }
//
//    //用户鉴权
//    @Bean
//    UserAuthRestInterceptor getUserAuthRestInterceptor() {
//        return new UserAuthRestInterceptor();
//    }

//    @Bean
//    BatchUploadInterceptor getBatchUploadInterceptor() {
//        return new BatchUploadInterceptor();
//    }


    /**
     * 需要用户和服务认证判断的路径
     * @return
     */
    private ArrayList<String> getIncludePathPatterns() {
        ArrayList<String> list = new ArrayList<>();
        String[] urls = {
                "/**",
                "/api/user/un/**"
        };
        Collections.addAll(list, urls);
        return list;
    }
    private ArrayList<String> getIncludePathPatternsCoal() {
        ArrayList<String> list = new ArrayList<>();
        String[] urls = {
                "/**"
        };
        Collections.addAll(list, urls);
        return list;
    }

    /**
     * 无需认证路径
     * @return
     */
    private ArrayList<String> getNotIncludePathPatterns() {
        ArrayList<String> list = new ArrayList<>();
        String[] urls = {
                "/jwt/**",
                "/api/log/save",
                "/noAuth/**",
//                "/code/**",
                "/api/user/validate",
                "/websocket/**",
                "/api/job/**",
                "/api/feign/**",
                "/api/user/switchTenant",
                "/api/loginLog/save",
                "/api/filerest/**",
                "/error",
                "/doc.html/**",
                "/favicon.ico",
                "/swagger/**",
                "/swagger-resources/**",
                "/webjars/**",
                "/v2/**",
                "/swagger-ui.html/**",
                "/apiOpen/**",
                "/api/userNotifys",
                "/userLogoff/**",
                "/tenantLogout/**",
                "/menjin/**",
                "/caiyangji/**",
                "/trade/**",
                "/appUpgrade/checkUpgrade"
        };
        Collections.addAll(list, urls);
        return list;
    }


}
